package cn.agiledata.bank.interest.service;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;
/***
 * 活期计息通过积数的概念计算利息，业务逻辑与定期等类型的不同
 * @author jessik
 * */
public class MutativeInterestCore extends InterestCore {
	final static Logger log = Logger.getLogger(MutativeInterestCore.class);
 	public MutativeInterestCore(){
 	}
 	/**
 	 * 计算积数
 	 * @param money 金额，不计元以下部分 
 	 * @param lastModified 该金额最后变动日期
 	 * */
 	public final static String  GetAccumulation_Money_LastModified = "getAccumulation(Money,LastModified)";
	public static final long getAccumulation(double money, Date lastModified) {
		//对于初始账户该记录字段可能为空
		if(lastModified==null)return 0;
		
		long moneyLong = new BigDecimal(money).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
		
		Date now = new Date();
		
		int days = getNaturalDays(lastModified,now);
		
		long accumulation = moneyLong*days;
		
		if(log.isDebugEnabled()){
			getBankDays(lastModified,now);
		}
		log.info(GetAccumulation_Money_LastModified+moneyLong+"("+money+")*"+days+"="+accumulation);
		
		return accumulation;
	}
	/***
	 * 计算利息
	 * 四舍五入保留到RMB分，即保留两位小数
	 * @param IRDaily 日利率值，不含百分号、千分号等
	 * @param annumulation 积数
	 * */
	public final static String  GetInterest_Ird_Accumulation = "getInterest(Ird,Accumulation)";
	public static final double getInterest(float IRDaily,long accumulation) {
		float interest = IRDaily*accumulation;
		
		double interest_format = new BigDecimal(interest).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		
		log.info(GetInterest_Ird_Accumulation+IRDaily+"*"+accumulation);
		
		return interest_format;
	}
	public static final double getInterest(Double IRDaily,long accumulation) {
		if(IRDaily==null){
			throw new RuntimeException("利率值(Double)==null无法计算."); 
		}
		return getInterest(IRDaily.floatValue(),accumulation);
	}
}
 
